var myError = require("./responObj.error");
var refresh_html_template = fs.readFileSync(__dirname + "/refresh.html.template").toString();

function ResponObj(type, obj) {
	type = type.toLowerCase();
	//TODO:加入压缩、加密功能，客户端将自动解压、解密
	var result;
	switch (type) {
		case "error":
			var args = Array.prototype.slice.call(arguments);
			//弹出第一个参数type
			args.shift();
			result = {
				type: "error",
				toString: myError.apply(null, args).toString()
			};
			break;
		case "json":
			result = {
				type: "json",
				toString: JSON.stringifySecurity(obj)
			};
			break;
		case "html":
			result = {
				type: "html",
				toString: obj
			};
			break;
		case "refresh":
			if (typeof obj === "string") {
				obj = {
					refresh_url: obj
				}
			};
			result = refresh_html_template.format(obj);
			break;
		default:
			result = {
				type: type,
				toString: String(obj)
			};
	};
	return result;
}

global.ResponObj = ResponObj;
module.exports = ResponObj;